package com.zkx.mvp.view.activity;

import android.content.Intent;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zkx.mvp.MainActivity;
import com.zkx.mvp.R;
import com.zkx.mvp.contract.LoginContract;
import com.zkx.mvp.contract.TestContract;
import com.zkx.mvp.persenter.LoginPresenter;
import com.zkx.mvp.persenter.TestPresenter;
import com.zkx.mvp.utils.ToastUtil;
import com.zkx.mvp.view.BaseActivity;

import butterknife.BindView;
import butterknife.OnClick;

import static com.zkx.mvp.R.id.response;

/**
 * Author：zhangkx on 2017/6/16 17:01
 * Email：zkx9025@163.com.
 * 登录
 */

public class LoginActivity extends BaseActivity implements LoginContract.View, TestContract.View {
    @BindView(R.id.email)
    AutoCompleteTextView email;
    @BindView(R.id.password)
    EditText password;
    @BindView(R.id.email_sign_in_button)
    Button emailSignInButton;
    @BindView(R.id.baidu_test)
    Button baiduTest;
    @BindView(response)
    TextView responseTv;

    @BindView(R.id.email_login_form)
    LinearLayout emailLoginForm;
    @BindView(R.id.activity_main)
    RelativeLayout activityMain;
    private LoginPresenter mLoginPresenter;
    private TestPresenter mTestPresenter;


    @Override
    protected int getContentLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        mLoginPresenter = new LoginPresenter();
        mTestPresenter = new TestPresenter();
        mLoginPresenter.attachView(this);
        mTestPresenter.attachView(this);

    }

    @Override
    protected void initData() {

    }


    @Override
    public void showError() {
        ToastUtil.showToast(this, "登录失败");
    }

    @Override
    public void complete() {
        ToastUtil.showToast(this, "登录成功！");
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }

    @Override
    public void onNameError() {
        email.setError("用户名错误");
    }

    @Override
    public void onPassWordError() {
        password.setError("密码错误");
    }


    @OnClick({R.id.email_sign_in_button, R.id.baidu_test})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.email_sign_in_button:
                mLoginPresenter.login(email.getText().toString().trim(), password.getText().toString().trim());
                break;
            case R.id.baidu_test:
                mTestPresenter.test(email.getText().toString().trim());
                break;
        }
    }

    @Override
    public void onFailed(String msg) {
        ToastUtil.showToast(this, msg);
    }

    @Override
    public void onResponse(String response) {
        responseTv.setText("Response:" + response.toString());
    }

    @Override
    public void onSuccess(String result) {
        responseTv.setText("Response:" + result);
    }
}
